/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hoabui
 */
public class frmQLNguoiDung extends javax.swing.JFrame {

    /**
     * Creates new form frmQLNguoiDung
     */
    public frmQLNguoiDung() {
        initComponents();
        loadDSLoaiNguoiDung();
        loadDSNguoiDung();
    }
    //Danh sach nguoi dung
   List<NguoiDung>dsNguoiDung=new ArrayList<NguoiDung>();
   //Danh sach loai nguoi dung
   List<LoaiNguoiDung>dsLND=new ArrayList<LoaiNguoiDung>();
   //Load danh sach nguoi dung len table
    private void loadDSNguoiDung(){
        //Tao model cho table
        DefaultTableModel model =new DefaultTableModel();
        //Them cot vao model
        model.addColumn("Username");
        model.addColumn("Password");
        model.addColumn("Họ tên");
        model.addColumn("Loại người dùng");
        //Lay danh sach nguoi dung
        dsNguoiDung=NguoiDung.layDanhSachNguoiDung();
        //Them dong vao model
        for(int i=0;i<dsNguoiDung.size();i++){
            NguoiDung nd=dsNguoiDung.get(i);
            Vector v=new Vector();
            v.add(nd.getUserName());
            v.add(nd.getPassword());
            v.add(nd.getHoTen());
            v.add(nd.getLoaiNguoiDung());
            model.addRow(v);
        }
        //Load model len table
        tblDanhSachNguoiDung.setModel(model);
    }
    //ham load danh sach loai nguoi dung len combobox
    private void loadDSLoaiNguoiDung(){
        //Lay danh sach loai nguoi dung
       dsLND=LoaiNguoiDung.layDanhSachLoaiNguoiDung();
        DefaultComboBoxModel model=new DefaultComboBoxModel();
        for(int i=0;i<dsLND.size();i++){
            LoaiNguoiDung lnd=dsLND.get(i);
            model.addElement(lnd.getTenLoai());
          
        }
        cmbLoaiNguoiDung.setModel(model);
       
    }
    
    private void resetForm(){
        tfHoTen.setText("");
        tfUsername.setText("");
        tfPassword.setText("");
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        tfUsername = new javax.swing.JTextField();
        tfHoTen = new javax.swing.JTextField();
        cmbLoaiNguoiDung = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblDanhSachNguoiDung = new javax.swing.JTable();
        btnThem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnCapNhat = new javax.swing.JButton();
        tfPassword = new javax.swing.JPasswordField();
        btnThoat = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        jScrollPane1.setViewportView(jTextPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cmbLoaiNguoiDung.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Username");

        jLabel2.setText("Password");

        jLabel3.setText("Họ tên");

        jLabel4.setText("Loại người dùng");

        tblDanhSachNguoiDung.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblDanhSachNguoiDung.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDanhSachNguoiDungMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblDanhSachNguoiDung);

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnCapNhat.setText("Cập nhật");
        btnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapNhatActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setText("QUẢN LÝ NGƯỜI DÙNG");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addGap(115, 115, 115))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnThem)
                                .addGap(18, 18, 18)
                                .addComponent(btnXoa)
                                .addGap(18, 18, 18)
                                .addComponent(btnCapNhat)
                                .addGap(18, 18, 18)
                                .addComponent(btnThoat))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(tfPassword)
                                        .addComponent(tfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                                    .addComponent(jLabel1))
                                .addGap(68, 68, 68)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(cmbLoaiNguoiDung, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(tfHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel4)))))
                        .addGap(0, 49, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbLoaiNguoiDung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnCapNhat)
                    .addComponent(btnXoa)
                    .addComponent(btnThoat))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblDanhSachNguoiDungMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDanhSachNguoiDungMouseClicked
        // TODO add your handling code here:
        int index=tblDanhSachNguoiDung.getSelectedRow();
        String username=tblDanhSachNguoiDung.getValueAt(index, 0).toString();
        String password=tblDanhSachNguoiDung.getValueAt(index, 1).toString();
        String hoTen=tblDanhSachNguoiDung.getValueAt(index, 2).toString();
        int loaiNguoiDung=Integer.parseInt( tblDanhSachNguoiDung.getValueAt(index, 3).toString());
        int viTri=-1;
        for(int i=0;i<dsLND.size();i++)
            if(loaiNguoiDung==dsLND.get(i).getMaLoai())
            {
                viTri=i;
                break;
            }
        tfUsername.setText(username);
        tfPassword.setText(password);
        tfHoTen.setText(hoTen);
        cmbLoaiNguoiDung.setSelectedIndex(viTri);
    }//GEN-LAST:event_tblDanhSachNguoiDungMouseClicked

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        frmLogin frm=new frmLogin();
        frm.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        String user_name=tfUsername.getText();
        String password=tfPassword.getText();
        String hoTen=tfHoTen.getText();
        int index=cmbLoaiNguoiDung.getSelectedIndex();
        int loai_nguoi_dung=dsLND.get(index).getMaLoai();
        if(user_name.trim().length()==0 || password.trim().length()==0||hoTen.trim().length()==0)
        {
            JOptionPane.showMessageDialog(null, "Vui lòng nhập liệu đầy đủ", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            return;
        }
        NguoiDung nd=new NguoiDung(user_name, password, hoTen, loai_nguoi_dung);
        if(NguoiDung.layNguoiDungTheoUsername(user_name)!=null)
        {
            JOptionPane.showMessageDialog(null, "Username đã tồn tại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            return;
        }
        if(nd.ThemNguoiDung())
        {
             JOptionPane.showMessageDialog(null, "Thêm người dùng thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
             loadDSNguoiDung();
        }
        else
        {
            
             JOptionPane.showMessageDialog(null, "Thêm người dùng thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        if(tfUsername.getText().length()>0){
            if(NguoiDung.XoaNguoiDung(tfUsername.getText()))
            {
                JOptionPane.showMessageDialog(null, "Xoá người dùng thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                loadDSNguoiDung();
            }
            else
                JOptionPane.showMessageDialog(null, "Xoá người dùng thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }
        else
            JOptionPane.showMessageDialog(null, "Vui lòng chọn người dùng để xoá", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        resetForm();
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapNhatActionPerformed
        // TODO add your handling code here:
            // TODO add your handling code here:
        String user_name=tfUsername.getText();
        String password=tfPassword.getText();
        String hoTen=tfHoTen.getText();
        int index=cmbLoaiNguoiDung.getSelectedIndex();
        int loai_nguoi_dung=dsLND.get(index).getMaLoai();
        if(NguoiDung.layNguoiDungTheoUsername(user_name)==null)
        {
            JOptionPane.showMessageDialog(null, "Username không tồn tại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            return;
        }
        if(user_name.trim().length()==0 || password.trim().length()==0||hoTen.trim().length()==0)
        {
            JOptionPane.showMessageDialog(null, "Vui lòng nhập liệu đầy đủ", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            return;
        }
        NguoiDung nd=new NguoiDung(user_name, password, hoTen, loai_nguoi_dung);
        if(nd.CapNhatNguoiDung())
        {
             JOptionPane.showMessageDialog(null, "Cập nhật người dùng thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
             loadDSNguoiDung();
        }
        else
        {
            
             JOptionPane.showMessageDialog(null, "Cập nhật người dùng thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }
        
    }//GEN-LAST:event_btnCapNhatActionPerformed


   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapNhat;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cmbLoaiNguoiDung;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTable tblDanhSachNguoiDung;
    private javax.swing.JTextField tfHoTen;
    private javax.swing.JPasswordField tfPassword;
    private javax.swing.JTextField tfUsername;
    // End of variables declaration//GEN-END:variables
}
